package com.example.demo.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: YKH
 * Date: 2023-01-05
 * Time: 16:04
 */
//登录的拦截器
@Configuration
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //设置为false,使session在不存在的时候,不去新建一个session
        HttpSession session = request.getSession(false);
        if( session!=null && session.getAttribute("userinfo")!=null){
            return true;
        }
        response.setStatus(401);
        return false;
    }
}
